﻿#include "dataparamwidget.h"
#if _MSC_VER >=1600
#pragma execution_character_set("utf-8")
#endif
QDataParamWidget::QDataParamWidget(QString sName,int nCmdIndex,QStringList sParamNamelist,QWidget *parent) : QWidget(parent)
{
    _nCmdIndex = nCmdIndex;
    _sParamNamelist = sParamNamelist;
    _sCmdName = sName;

    InitUI();

    connect(_pSendDataBtn,&QPushButton::clicked,this,&QDataParamWidget::onSendData);

    QUIQss::setBtnQss(_pSendDataBtn,"#1ABC9C", "#E6F8F5", "#2EE1C1", "#FFFFFF", "#16A086", "#A7EEE6");

}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QDataParamWidget::InitUI()
{
    QLabel* pWidgetNamalabel= new QLabel(this);
    pWidgetNamalabel->setText(_sCmdName+"参数");
    pWidgetNamalabel->setStyleSheet("font:20px 'Microsoft YaHei';color:white");
    pWidgetNamalabel->setMaximumHeight(30);

    QGridLayout* playout = new QGridLayout(this);

    int nRow = 0;
    playout->addWidget(pWidgetNamalabel,nRow,0,1,2);
    nRow++;


    for(int i = 0; i < _sParamNamelist.size();i++)
    {
        QLabel* pNameLabel= new QLabel(this);
        pNameLabel->setText(_sParamNamelist[i]);
        pNameLabel->setStyleSheet("font:16px 'Microsoft YaHei';color:white");
        pNameLabel->setMaximumHeight(30);

        QLineEdit* pParamValueEdit = new QLineEdit("0.0");
        QUIQss::setTxtQss(pParamValueEdit, "#DCE4EC", "#1ABC9C");

        _pValueEditList.push_back(pParamValueEdit);

        playout->addWidget(pNameLabel,nRow,0,1,1);
        playout->addWidget(pParamValueEdit,nRow,1,1,2);
        nRow++;
    }

    _pSendDataBtn = new QPushButton("发送数据");
    _pSendDataBtn->setStyleSheet(
                "QPushButton:checked { background-color: rgb(26,188,156);color:white;}"
                "QPushButton:unchecked { background-color: rgb(255,255,255);;color:black;}"
                "QPushButton{background-color: rgb(255,255,255);padding:5px;border-radius:5px;color:black;"
                "border-style: outset;"
                "border-width: 0px;border-color: gray; "
                "font-size:25px;font-family: 'Microsoft YaHei';}"
                                );

    playout->addWidget(_pSendDataBtn,nRow-1,3,1,1);

    for(int i = 0; i < nRow;i++)
    {
        playout->setRowStretch(i,1);
    }

}


/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
bool QDataParamWidget::setParamData(QList<float> pValueList)
{
    _pValueList.clear();

    for(int i = 0; i < pValueList.size();i++)
    {
        _pValueEditList[i]->setText(QString("%1").arg(pValueList[i]));
        _pValueList.append(pValueList[i]);
    }

    return true;
}


/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
bool QDataParamWidget::saveParamData()
{
    _pValueList.clear();

    for(int i = 0; i < _pValueEditList.size();i++)
    {
        _pValueList.append(_pValueEditList[i]->text().toFloat());
    }

    return true;
}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QDataParamWidget::onSendData()
{
    this->saveParamData();

    int datalen = sizeof(float)*_pValueList.size();

    QByteArray l_data;
    l_data.resize(datalen);
    uchar* pdata = (uchar*)l_data.data();

    for(int i = 0; i < _pValueList.size();i++)
    {
        float lfdata = _pValueList[i];
        memcpy(pdata+sizeof(float)*i,(uchar*)&lfdata,sizeof(float));
    }

    emit signal_set_data_value(_nCmdIndex,l_data,sizeof(float));
}
